# Maintainer: Sergej Pupykin <pupykin.s+arch@gmail.com>

pkgname=libsieve
pkgver=2.3.1
pkgrel=5
pkgdesc="sieve interpretator lib"
arch=(loong64 x86_64)
url="http://libsieve.sourceforge.net/"
license=('GPL')
source=("https://github.com/downloads/sodabrew/libsieve/libsieve-$pkgver.tar.gz"
	"glibc-regex.patch"
	"crash-fix.patch")
sha512sums=('6d03f48daf8b85723f04efe673bb246ae79320b1e7862b81d17ea0fe7941b9057012cd217d0593ecd15192ecef572d83d57051a36388e53028bf018c3c5a792f'
            'c58e2f5a5d78748e49dee20b2c9e73e290bff844619bd625e02368164be12907923cbc659ea726e33deccf2a1c4f446f49b50f7f014d49e75ce5eec192480a14'
            '9b7b3254027a27290ed5aaed5d4d801ebf1bc51f0d3a3dcdbadf82fc66d00f20cadd54aeb370ed1932f907cf1db57a85acb6473ddd45adc29705233a93b7a762')

prepare() {
  cd "$srcdir/$pkgname-$pkgver"
  rm -rf src/sv_regex
  rm -f src/sv_parser/{addr,header,sieve}.{c,h}
  rm -f src/sv_parser/{addr,header,sieve}-lex.{c,h}
  patch -p1 <"$srcdir"/glibc-regex.patch
  patch -p1 <"$srcdir"/crash-fix.patch
}

build() {
  cd "$srcdir/$pkgname-$pkgver"
  aclocal
  automake
  autoconf
#  export CFLAGS="-g -O2"
#  export LDFLAGS="-g -O2"
  ./configure --prefix=/usr
  (cd src/sv_parser && for i in `ls -1 *.l | sed 's|\.l||'`; do flex --header-file=$i.h -o $i.c $i.l; done)
  make
}

check() {
  cd "$srcdir/$pkgname-$pkgver/src/sv_test"
  export LD_LIBRARY_PATH="$srcdir"/libsieve-$pkgver/src/.libs:$LD_LIBRARY_PATH
  ls -1 script* | while read script; do
    ls -1 message* | while read msg; do
      if [ "$script" == "script22.sv" ]; then
        # script22.sv: syntax errors check
        ./example $script $msg && false || true
      else
        if [ "$script" == "script23.sv" ]; then
          # script23.sv: enotify looks unsupported or I don't know how to use it - skip it
          true
        else
          ./example $script $msg
        fi
      fi
    done
  done
}

package() {
  cd "$srcdir/$pkgname-$pkgver"
  make DESTDIR="$pkgdir/" install
}
